package com.ruoyi.web.controller.tool.xml;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.*;

public class ExportDataToFiles {

    // 数据库配置
    private static final String DB_URL = "jdbc:mysql://localhost:3306/test";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "password01!";

    // 输出目录
    private static final String OUTPUT_DIR = "C:/output_files";
    private static final int ROWS_PER_FILE = 10;

    public static void main(String[] args) {
        // 创建输出目录
        java.io.File outputDir = new java.io.File(OUTPUT_DIR);
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            // 获取总行数
            int totalRows = getTotalRows(conn);

            // 计算需要生成的文件数量
            int fileCount = (totalRows + ROWS_PER_FILE - 1) / ROWS_PER_FILE;

            // 分页查询并导出
            for (int i = 0; i < fileCount; i++) {
                int offset = i * ROWS_PER_FILE;
                String outputFile = OUTPUT_DIR + "/FILENAME.p" + String.format("%03d", (i + 1));

                try (FileWriter writer = new FileWriter(outputFile)) {
                    // 写入文件开头
                    writer.write("|||BEGIN|||\n");

                    // 查询数据
                    String query = "SELECT filed1, filed2, filed3, filed4 FROM xml_in LIMIT ? OFFSET ?";  // 替换为你的表名和字段
                    try (PreparedStatement stmt = conn.prepareStatement(query)) {
                        stmt.setInt(1, ROWS_PER_FILE);
                        stmt.setInt(2, offset);
                        try (ResultSet rs = stmt.executeQuery()) {
                            int rowIndex =  1;
                            while (rs.next()) {
                                // 构造行数据
                                String line = rowIndex + " " +
                                        rs.getString("filed1") + "\u007F" + "|" + "\u007F"+
                                        rs.getString("filed2") + "\u007F" + "|" + "\u007F" +
                                        rs.getString("filed3") + "\u007F" + "|" + "\u007F" +
                                        rs.getString("filed4") + "\u007F" + "|" + "\u007F"+"^ENDROW"+"\n";
                                writer.write(line);
                                rowIndex++;
                            }
                        }
                    }

                    // 写入文件结尾
                    writer.write("|||END|||\n");
                }

                System.out.println("Exported " + outputFile);
            }
        } catch (SQLException | IOException e) {
            e.printStackTrace();
        }
    }

    // 获取总行数
    private static int getTotalRows(Connection conn) throws SQLException {
        String query = "SELECT COUNT(*) FROM xml_in";  // 替换为你的表名
        try (Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(query)) {
            if (rs.next()) {
                return rs.getInt(1);
            }
        }
        return 0;
    }
}